package com.lifuping.fileupload.controller;


import com.lifuping.fileupload.service.MinioStorageService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("oss")
@AllArgsConstructor
public class OssController {

    private final MinioStorageService minioStorageService;

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new Exception("上传文件不能为空");
        }

        Date date = new Date();
        String dataForm = new SimpleDateFormat("yyyy-MM-dd").format(date);

        //上传文件
        String path = dataForm+"/"+file.getOriginalFilename();
        String url = minioStorageService.uploadFile(file.getBytes(),path);
        return url;
    }

    /**
     * 删除
     */
    /*@RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id){
        SysOss oss = minioStorageService.getById(id);
        ossFactory.build().delete(oss.getUrl());
        sysOssService.removeById(id);
        return R.ok();
    }*/

}

